package com.tsd.jxc.sale.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.req.JxcSaleOrderDetailReqParamsVo;
import com.tsd.jxc.req.JxcSaleReturnBillDetailReqParamsVo;
import com.tsd.jxc.sale.entity.JxcSaleOrderDetail;
import com.tsd.jxc.sale.entity.JxcSaleOrderDetailExt;
import com.tsd.jxc.sale.service.JxcSaleOrderDetailService;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SystemSettingService;
import com.tsd.system.vo.GlobalSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcSaleOrderDetail销售订单-明细 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:04:27
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-销售订单-明细", description = "销售订单-明细的API描述")
public class JxcSaleOrderDetailController extends BaseController {

    @Resource
    private JxcSaleOrderDetailService jxcSaleOrderDetailService;
    @Resource
    private SystemSettingService systemSettingService;
    @Resource
    private JxcSettingService jxcSettingService;

    @ApiOperation(value = "根据sid获取销售订单-明细", notes = "根据sid获取销售订单-明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "销售订单-明细sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcSaleOrderDetailBySid.action", method = RequestMethod.POST)
    public HttpResult<JxcSaleOrderDetail> getJxcSaleOrderDetailBySid() {
        HttpResult<JxcSaleOrderDetail> hr = new HttpResult<JxcSaleOrderDetail>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcSaleOrderDetail jxcSaleOrderDetail = jxcSaleOrderDetailService.queryBySid(sid);
            hr.setData(jxcSaleOrderDetail);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除销售订单-明细", notes = "根据id删除销售订单-明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "销售订单-明细sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcSaleOrderDetails.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcSaleOrderDetails() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcSaleOrderDetailService.deleteJxcSaleOrderDetails(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存销售订单-明细", notes = "保存销售订单-明细，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "销售订单-明细", required = true, paramType = "body", dataType = "JxcSaleOrderDetail")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcSaleOrderDetail.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcSaleOrderDetail() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcSaleOrderDetail t = JSON.parseObject(json, JxcSaleOrderDetail.class);
            jxcSaleOrderDetailService.saveJxcSaleOrderDetail(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询销售订单-明细", notes = "分页查询销售订单-明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcSaleOrderDetails.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcSaleOrderDetails() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<JxcSaleOrderDetailExt> list = jxcSaleOrderDetailService.findJxcSaleOrderDetails(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询以销定购列表", notes = "分页查询以销定购列表")
    @ResponseBody
    @RequestMapping(value = "/findJxcSaleOrderDetails4Sale2Buy.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcSaleOrderDetails4Sale2Buy() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyDateRangeValue(map, params, "order_date", false);
            HlpUtils.copyStringValue(map, params, "client_sid", "client_sid", false, false);
            HlpUtils.copyListValue(map, params, "client_list", "client_list", false, String.class);
            HlpUtils.copyListValue(map, params, "goods_list", "goods_list", false, String.class);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            if (map.containsKey(Constants.KEY_NO_PAGE)) {
                page = null;
            }
            List<JxcSaleOrderDetailExt> list = jxcSaleOrderDetailService.findJxcSaleOrderDetails4Sale2Buy(params, page);
            if (!HlpUtils.isEmptyList(list)) {
                GlobalSetting setting = systemSettingService.getGlobalSetting();
                ListUtil.forEach(list, t -> setting.procDisplay(t, GlobalSetting.TYPE_CLIENT));
            }
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            if (settingVo.isClothing()) {
                hr.setData(JxcSaleOrderDetailReqParamsVo.transfer(list, settingVo));
            } else {
                hr.setData(list);
                hr.setPage(page);
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "销售退货单选单", notes = "销售退货单选单")
    @ResponseBody
    @RequestMapping(value = "/queryJxcSaleOrderDetailList4Return.action", method = RequestMethod.POST)
    public HttpResult<Object> queryJxcSaleOrderDetailList4Return() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "client_sid", "client_sid", true, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            HlpUtils.copyListValue(map, params, "selected_list", "selected_list", false, String.class);
            PageBean page = new PageBean(map);
            if (map.containsKey(Constants.KEY_NO_PAGE)) {
                page = null;
            }
            List<JxcSaleOrderDetailExt> list = jxcSaleOrderDetailService.queryJxcSaleOrderDetailList4Return(params, page);
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            if (settingVo.isClothing()) {
                hr.setData(JxcSaleReturnBillDetailReqParamsVo.transfer(list, settingVo));
            } else {
                hr.setData(list);
            }
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
